<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">{{ company.manager }}</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepts(company)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="departs" :props="defaultProps">
          <template #default="{data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleClickDepts($event,data)">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                        <el-dropdown-item command="edit">修改部门</el-dropdown-item>
                        <el-dropdown-item command="del">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- sync表示父组件把传给子组件的值，放权给子组件去修改 -->
      <add-depts ref="addDepts" :show-dialog.sync="showDialog" :node-date="nodeDate" :depart-list="departList" @add-success="getDepartmentsList" @closeDialogFn="closeDialog" />
    </div>
  </div>
</template>

<script>
import { getDepartmentsListAPI, delDepartmentsListAPI } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import addDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: { addDepts },
  data() {
    return {
      // label 表示的当前节点的文本
      // children:表示的当前节点有没有子级
      company: {
        id: '',
        name: '',
        manager: '负责人',
        children: []
      },
      showDialog: false,
      departs: [],
      defaultProps: {
        label: 'name'
      },
      nodeDate: {},
      departList: [],
      isLoading: false
    }
  },
  created() {
    this.getDepartmentsList()
  },
  methods: {
    async getDepartmentsList() {
      this.isLoading = true
      const { data } = await getDepartmentsListAPI()
      /* console.log(data) */
      this.company.name = data.companyName
      this.departList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      this.company.children = this.departs
      this.isLoading = false
    },
    handleClickDepts(command, data) {
      if (command === 'add') {
        this.addDepts(data)
      }
      if (command === 'edit') {
        this.editDepts(data)
      }
      if (command === 'del') {
        this.delDepts(data.id)
      }
    },
    editDepts(data) {
      this.showDialog = true
      this.nodeDate = data
      /* this.$refs.addDepts.showTitle = '编辑部门' */
      this.$nextTick(() => {
        this.$refs.addDepts.getDepartmentDetail()
      })
    },
    delDepts(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartmentsListAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getDepartmentsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addDepts(data) {
      this.showDialog = true
      this.nodeDate = data
      /* this.$refs.addDepts.showTitle = '添加部门' */
      /* console.log(this.$refs.addDepts) */
      /* this.$refs.addDepts.getUserSimpleList() */
    },
    closeDialog() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
